package com.fourth.service.impl;

import com.fourth.config.WxPayConfiguration;
import com.fourth.service.UbWxPayService;
import com.fourth.util.HttpClient;
import com.fourth.util.PayUtil;
import com.fourth.vo.PayInfoVO;
import com.github.wxpay.sdk.WXPayUtil;
import com.ljx.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mr.ma
 * @since 2023/3/8 16:15
 **/
@Service
public class UbWxPayServiceImpl implements UbWxPayService {

    @Resource
    private WxPayConfiguration wxPayConfiguration;

    @Resource
    private PayUtil payUtil;


    @Override
    public PayInfoVO wxPay(String outTradeNo, String subjectName, BigDecimal totalAmount) {
        // 请求参数的封装
        Map<String, String> params = new HashMap<>(64);
        params.put("appid", wxPayConfiguration.getAppid());
        params.put("mch_id", wxPayConfiguration.getMchid());
        params.put("nonce_str", WXPayUtil.generateNonceStr());
        params.put("body", subjectName);
        params.put("out_trade_no", outTradeNo);
        params.put("total_fee", new BigDecimal(0.01).multiply(new BigDecimal(100)).longValue() + "");

        // 真实服务器的ip
        params.put("spbill_create_ip", "127.0.0.1");
        // 支付成功后跳转的路径
        params.put("notify_url", wxPayConfiguration.getNotifyUrl());

        params.put("trade_type", "NATIVE");

        try {
            //2.远程调用微信支付接口
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            //设置支持https协议的请求
            httpClient.setHttps(true);
            //设置xml格式的参数
            httpClient.setXmlParam(WXPayUtil.generateSignedXml(params, wxPayConfiguration.getApiKey()));
            //发送请求
            httpClient.post();

            //获取返回的结果
            String content = httpClient.getContent();
            //把xml转化为map
            Map<String, String> map = WXPayUtil.xmlToMap(content);
            if (map.get("result_code").equals("SUCCESS")) {
                PayInfoVO payInfoVO = new PayInfoVO();
                payInfoVO.setPrice(totalAmount.toString());
                payInfoVO.setOrderNo(outTradeNo);
                payInfoVO.setCodeUrl(map.get("code_url"));
                return payInfoVO;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Result queryPayStatus(String code) {
        Map<String, String> params = new HashMap<>();
        params.put("appid", wxPayConfiguration.getAppid());
        params.put("mch_id", wxPayConfiguration.getMchid());
        params.put("out_trade_no", code);
        params.put("nonce_str", WXPayUtil.generateNonceStr());
        try {
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            httpClient.setHttps(true);
            httpClient.setXmlParam(WXPayUtil.generateSignedXml(params, wxPayConfiguration.getApiKey()));

            httpClient.post();
            String content = httpClient.getContent();

            Map<String, String> map = WXPayUtil.xmlToMap(content);
            if (map.get("trade_state").equals("SUCCESS")) {
                return payUtil.paySuccess(code,1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(5000, "订单未支付");
    }
}
